package com.hare.harelearn.DataStructure.ArrayAlgorithm;

import java.util.Arrays;

/**
 * @program: HareLearn
 * @description:
 * @author: Hare
 * @create: 2025−03-08 22:46
 * @Version 1.0
 **/
public class 删除有序数组的重复项 {

    public static void main(String[] args) {

        System.out.println(removeDuplicates(new int[]{1, 2, 2, 2, 3, 4, 4, 5, 5}));
        //System.out.println(removeDuplicates(new int[]{1, 1,1,1,1, 2, 2, 3, 4, 4, 5, 5,6,6,7,8,9,9,10}));

    }


    /**
     * 双指针法则
     * fast指针指向循环数组的下标从1开始
     * ++slow指针指向不同元素要插入的位置，每次++
     * 举例栗子：比如数组[1,2,2,3,3,4,4] slow[0]=1,fast[1]=2 所以1!=2 我们++slow作为不同元素待插入位置即nums[++slow] = nums[fast];
     * @param nums
     * @return
     */
    public static int removeDuplicates(int[] nums) {
        int slow = 0, fast = 1;
        while (fast < nums.length) {
            if(nums[slow]!= nums[fast]){
                nums[++slow] = nums[fast];
            }
            fast++;
        }
        System.out.println(Arrays.toString(nums));
        return slow+1;
    }
}
